class WelcomeController < ApplicationController

  autocomplete :part, :name, :full => true
  autocomplete :city, :name, :full => true, :display_value => :extended_city_name
  
  def show
    @request = Request.new
    
    if current_user
      @request.city_id = current_user.city_id
      @request.email = current_user.email
      @request.gsm = current_user.gsm
    end
  end
  
  def home
    
  end
  
  def update_car_model_select
    car_models = CarModel.where(:car_brand_id => params[:id]).order(:name) unless params[:id].blank?
    render :partial => "car_models", :locals => { :car_model_id => nil, :car_models => car_models }    
  end
  
  def show2
    if params[:request][:gsm] == "GSM"
      params[:request][:gsm] = nil
    end
    if params[:request][:email] == "e-mail"
      params[:request][:email] = nil
    end
    
    @request = Request.new(params[:request])
    @request.cart = current_cart
    current_cart.request = @request
    
    if !params[:region_ids].nil?
      params[:region_ids].each do |i|
        @request.region_requests.build(:region_id => i)
      end
    end
    
    @request.is_authorized = false
        
    respond_to do |format|
      if @request.save
        session[:cart_id] = nil
        format.html { redirect_to(root_url, :notice => 'Request successfully sent.') }
      else
        format.html { render "show" }
      end
    end    
  end

end
